'use strict';
// 防止重复提交

angular.module("preventRepeatSubmit", []).factory('preventRepeatSubmit', ['$q', '$injector', function($q, $injector) {
    function lock() {
        var currentSubmit = $("body form").find("button[type='submit'][submit-loading='true']");
        currentSubmit.text("loading...");
        currentSubmit.attr('disabled', true);
    }

    function unLock() {
        var currentSubmit = $("body form").find("button[type='submit'][submit-loading='true']");
        var oldText = currentSubmit.attr('submit-old-text');
        currentSubmit.text(oldText);
        currentSubmit.removeAttr('disabled');
        currentSubmit.removeAttr('submit-loading');
        currentSubmit.removeAttr('submit-old-text');
    }
    var preventRepeatSubmit = {
        'responseError': function(response) {
            unLock();
            return $q.reject(response);
        },
        'response': function(response) {
            unLock();
            // debugger
            return response;
        },
        'request': function(config) {
            lock();
            return config;
        },
        'requestError': function(config) {
            return $q.reject(config);
        }
    }
    return preventRepeatSubmit;
}])
